//
//  BIAS.cpp
//  Chart
//
//  Created by CaoJun on 13-7-15.
//
//

#include "BIAS.h"

using namespace ytx;
using namespace std;

BIAS::BIAS(IndexConfig indexConfig):IndexData(indexConfig, 2)
{
    type = QuotationType::BIAS;
    indexName = "BIAS";
    lineData.assign(1, vector<double>());
    this->setColumn(Rect(2,5,5));
}

BIAS::~BIAS()
{

}

void BIAS::updateLastest(ytx::Quotation *data){
    int n = para.value[0];

    n = (int) Tools::between(para.range[0].first, para.range[0].second, n);

    int j = data->getDataLength();

    if (j < n){
        return;
    }


    double CN = computeMA(data, n, &KLineItem::close, j-n)[0].back();
    double c = data->kLineData[j-1].close;
    double BIAS = (c - CN) / CN * 100;

    lineData[0].back() = BIAS;
}

void BIAS::calculate(ytx::Quotation *data){
    lineData.clear();

    int n = para.value[0];

    n = (int) Tools::between(para.range[0].first, para.range[0].second, n);

    int i = n, j = data->getDataLength();

    if (j < n){
        return;
    }

    vector<double> BIAS(n);
    vector<double> CN = computeMA(data, n, &KLineItem::close)[0];

    BIAS = validation(n, BIAS);

    double c;

    while (i < j) {
        c = data->kLineData[i].close;
        BIAS.push_back((c - CN[i]) / CN[i] * 100);
        i++;
    };
    lineData.push_back(BIAS);

}

string BIAS::getCrosshairYData(int index)
{
    return Tools::Convert2FixString(lineData[0][index], toFix);
}

int BIAS::getDataLength() const
{
    return hasData() ? (int)lineData[0].size() :0;
}

bool BIAS::hasData() const
{
    return (lineData.size() > 0 && lineData[0].size() > 0);
}

bool BIAS::hasData(int index) const
{

    if (hasData())
    {
        int count = (int)lineData[0].size();
        return 0 <= index && index < count;
    }
    else{
        return false;
    }
}

std::vector<std::pair<std::string, std::string> > BIAS::getTopSideText(int index) const
{
    return Quotation::getTopSideText(lineData, index);
}